using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class BackGroundFollow : MonoBehaviour
{
    public Camera camera;
    [SerializeField] private float parallaxEffect=1;
    private float xPosition;
    private float length;
    private void Start()
    {
        camera = Camera.main;
        xPosition = transform.position.x;
        length = GetComponent<SpriteRenderer>().bounds.size.x;
    }
    private void Follow()
    {
        float distanceToMove = camera.gameObject.transform.position.x * parallaxEffect;
        transform.position = new Vector3(xPosition + distanceToMove, transform.position.y);
        float distanceMoved = camera.gameObject.transform.position.x * (1- parallaxEffect);
        if (distanceMoved > xPosition + length)
            xPosition += length;
        else if (distanceMoved < xPosition - length)
            xPosition -= length;
    }
    private void Update()
    {
        Follow();
    }
}
